package com.bskj.sj.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.dao.BmsUserDao;
import com.bskj.common.dao.ParkingLotCustomerDao;
import com.bskj.common.dao.ParkingShopDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.StringUtils;
import com.bskj.sj.service.IBmsUserService;

/**
 * Created by mocaijin on 21/12/2017.
 */
@Service
public class BmsUserServiceImpl extends BaseServiceImpl<BmsUser> implements IBmsUserService {

    @Autowired
    private BmsUserDao bmsUserDao;

    @Autowired
    private ParkingShopDao parkingShopDao;

    @Autowired
    private ParkingLotCustomerDao parkingLotCustomerDao;

    @Override
    public BmsUser get(BmsUser BmsUser) throws Exception {

        FindRequestEntity findReq = new FindRequestEntity();
        if (BmsUser.getId() != null) {
            findReq.addWhere("id", WhereEnum.EQ, BmsUser.getId());
        }
        if (!StringUtils.isBank(BmsUser.getAppId())) {
            findReq.addWhere("app_id", WhereEnum.EQ, BmsUser.getAppId());
        }
        if (!StringUtils.isBank(BmsUser.getOpenId())) {
            findReq.addWhere("open_id", WhereEnum.EQ, BmsUser.getOpenId());
        }
        if (BmsUser.getCustId() != null) {
            findReq.addWhere("cust_id", WhereEnum.EQ, BmsUser.getCustId());
        }
        if (!StringUtils.isBank(BmsUser.getEntNo())) {
            findReq.addWhere("ent_no", WhereEnum.EQ, BmsUser.getEntNo());
        }
        if (!StringUtils.isBank(BmsUser.getPassword())) {
            findReq.addWhere("password", WhereEnum.EQ, BmsUser.getPassword());
        }

        List<BmsUser> BmsUserList = find(findReq);
        if (BmsUserList != null && BmsUserList.size() > 0) {
            return BmsUserList.get(0);
        }
        return null;
    }

	@Override
    public Map<String, Object> bindWx(BmsUser bmsUser) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        FindRequestEntity findReq = new FindRequestEntity();
        findReq.addWhere("ent_no", WhereEnum.EQ, bmsUser.getEntNo());
        findReq.addWhere("password", WhereEnum.EQ,EncryptUtil.getMD5(bmsUser.getPassword()));
        List<BmsUser> bmsUserList = find(findReq);
        if (bmsUserList == null || bmsUserList.size() == 0) {
            resultMap.put("error", "无效账号或者密码输入错误!");
            return resultMap;
        } else if (bmsUserList.size() >= 2) {
            resultMap.put("error", "存在重复账号，请联系管理员");
            return resultMap;
        }

        
        BmsUser result = bmsUserList.get(0);
        LogUtils.debug(JSONObject.toJSONString(result));
        if (result != null&&StringUtils.isBank(result.getAppId())&&StringUtils.isBank(result.getOpenId())
        		) {
        	result.setAppId(bmsUser.getAppId());
        	result.setOpenId(bmsUser.getOpenId());
            bmsUserDao.update(result);
            resultMap.put("authInfo", result);
        } else {
            resultMap.put("error", "已经被绑定!");
        }
        return resultMap;
    }

    @Override
    public boolean unBindWx(BmsUser bmsUser) {
        int i = bmsUserDao.unBindWx(bmsUser);
        if (i == 1) {
            return true;
        }
        return false;
    }

    @Override
    public BmsUser getByCustId(Integer custId) throws Exception {
        BmsUser bmsUser = new BmsUser();
        FindRequestEntity findReq = new FindRequestEntity();
        findReq.addWhere("cust_id", WhereEnum.EQ, custId);
        List<BmsUser> BmsUsers = find(findReq);
        if (BmsUsers != null && BmsUsers.size() >= 1) {
        	bmsUser = BmsUsers.get(0);
        	bmsUser = reset(bmsUser);
        }
        return bmsUser;
    }

    private BmsUser reset(BmsUser BmsUser) {
        Integer custId = BmsUser.getCustId();
        Integer type = BmsUser.getType();
        //1:停车场id，2:商户id,3.运营人员 id
        //1.物业公司账号显示物业公司名字 2.是运营人员显示物业名称+运营人员，3.商家显示物业名称+商家名称
        if (type == 1) {
            ParkingLotCustomer parkingLotCustomer = parkingLotCustomerDao.get(custId);
            BmsUser.setName(parkingLotCustomer.getName());
            BmsUser.setParkingLotCustomer(parkingLotCustomer);
        } else if (type == 2) {//商家显示物业名称+商家名称
        	System.out.println(BmsUser.getShopId());
            ParkingShop parkingShop = parkingShopDao.get(BmsUser.getShopId());
            System.out.println(parkingShop);
            ParkingLotCustomer parkingLotCustomer = parkingLotCustomerDao.get(parkingShop.getParkingLotCustomerId());
            BmsUser.setParkingShop(parkingShop);
            BmsUser.setName(parkingLotCustomer.getName() + "-" + parkingShop.getName());
        } else if (type == 3) {
            ParkingLotCustomer parkingLotCustomer = parkingLotCustomerDao.get(custId);
            BmsUser.setName(parkingLotCustomer.getName() + " 运营人员");
        }

        return BmsUser;
    }

    @Override
    public BmsUser getByAuthId(Integer authId) throws Exception {
        BmsUser BmsUser = get(authId);
        return reset(BmsUser);
    }
    
    

    @Override
	public BmsUser findUser(String appid, String openid) throws Exception {
    	FindRequestEntity findParam = new FindRequestEntity();
    	findParam.addWhere("app_id", WhereEnum.EQ, appid);
    	findParam.addWhere("open_id", WhereEnum.EQ, openid);
    	
    	List<BmsUser> bmsUserList = find(findParam);
    	if(bmsUserList != null  && bmsUserList.size() > 0) {
    		return bmsUserList.get(0);
    	}
		return null;
	}
    
    
    
	@Override
    public BaseDao<BmsUser> getBaseDao() {
        return bmsUserDao;
    }

	@Override
	public ParkingShop findShop(Integer shopId) throws Exception {
		ParkingShop parkingShop=parkingShopDao.get(shopId);
		return parkingShop;
	}
}
